'use strict'

module.exports = angular.module 'lib.filters.common', []
.filter 'parseInt', ($log)->
  return (str)->
    return "-" if isNaN( str )
    return parseInt(str)
.filter 'parseFloat', ($log)->
  return (str)->
    return "-" if isNaN( str )
    return parseFloat(str)
.filter 'daysFromNow', ($log)->
  return (time)->
    now = new Date().getTime()
    return Math.floor((now - time) / ( 1000 * 60 * 60 * 24 ))
# begin: 给format-as-currency定制的两个filter
.filter 'RMBCurrencyZeroFraction', ($log, $filter)->
  return (value)->
    return $filter('currency')(value, '¥', 0)
.filter 'RMBCurrencyTwoFraction', ($log, $filter)->
  return (value)->
    return $filter('currency')(value, '¥', 2)
# end: 给format-as-currency定制的两个filter
.filter 'inArray', ($log)->
  return (val, arr)->
    return if _.indexOf(arr, val) is -1 then false else true
.filter 'cut', ($log)->
  return(value,arr)->
    max = arr
    if value.length > max
      return value.substr(0,max)+'...'
    else
      return value

